package hoan.com.test;

import hoan.com.test.model.Book;
import hoan.com.test.service.BookLocalServiceUtil;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.liferay.portal.NoSuchUserException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class HoanPortlet
 */
public class HoanPortlet extends MVCPortlet {

	/**
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */
	public void updateBook(ActionRequest actionRequest,
			ActionResponse actionResponse) {

		String bookTitle = ParamUtil.getString(actionRequest, "bookTitle");
		String author = ParamUtil.getString(actionRequest, "author");
		try {
			String userId = actionRequest.getRemoteUser();
			BookLocalServiceUtil.addBook(Long.valueOf(userId), bookTitle);
		} catch (NoSuchUserException e) {
			e.printStackTrace();
		} catch (SystemException e) {
			e.printStackTrace();
		}
		System.out.println("Your inputs ==> " + bookTitle + ", " + author);
	}
	
	/**
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */
	public void getAllBooks(ActionRequest actionRequest, ActionResponse actionResponse){
		
		List<Book> bookList = BookLocalServiceUtil.findAllBooks();
		actionRequest.setAttribute("bookList", bookList);
		Locale locale = actionRequest.getLocale();
		actionRequest.setAttribute("book", HtmlUtil.escape(bookList.get(0).getTitle(locale)));
		SessionMessages.add(actionRequest, "loadBookListSuccessfully");
		SessionErrors.add(actionRequest, "error");
		actionResponse.setRenderParameter("jspPage", "list-jsp");
	}
	
	/**
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */
	public void getAllBooksByUserId(ActionRequest actionRequest, ActionResponse actionResponse){
		
		String userId = actionRequest.getRemoteUser();
		List<Book> bookList = BookLocalServiceUtil.findAllBooksByUserId(Long.valueOf(userId));
		PortalUtil.getHttpServletRequest(actionRequest).getSession().setAttribute("bookList", bookList);
		// actionRequest.setAttribute("book",
		// HtmlUtil.escape(bookList.get(0).getTitle()));
		// actionRequest.setAttribute("bookList", bookList);
		actionResponse.setRenderParameter("jspPage", "list-jsp");
	}

	/**
	 * 
	 * @param bookId
	 */
	public void deleteBookByBookId(ActionRequest actionRequest, ActionResponse actionResponse){
		
		String bookId = ParamUtil.getString(actionRequest, "bookId");
		
		try {
			BookLocalServiceUtil.deleteBook(Long.valueOf(bookId));
		} catch (PortalException e) {
			e.printStackTrace();
		} catch (SystemException e) {
			e.printStackTrace();
		}
		getAllBooksByUserId(actionRequest, actionResponse);
	}
	
	@Override
	public void render(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {

		String renderPageName = ParamUtil.get(request, "jspPage", "view-template");
		String renderPagePath = this.getInitParameter(renderPageName);
		this.include(renderPagePath, request, response);
	}
	
}
